/**
 * 
 */
package org.gaming.prefab.thing.bign;

import java.math.BigDecimal;
import java.util.List;

import org.gaming.db.repository.BaseRepository;
import org.gaming.prefab.IGameCause;
import org.gaming.prefab.thing.cost.CostReceipt;
import org.gaming.prefab.thing.cost.ICostThing;
import org.gaming.prefab.thing.reward.ExpRewardReceipt;
import org.gaming.prefab.thing.reward.RewardReceipt;

/**
 * @author YY
 * 数值资源类的储存器
 */
public abstract class ExpThingStorer<Log extends ExpThingLog> extends BignThingStorer<ExpRewardReceipt<BigDecimal>, CostReceipt<BigDecimal>> {
	
	protected abstract Log newLog();
	protected abstract BaseRepository<Log> findLogRepository(int dbZone);
	
	@Override
	protected final void afterAdd(long entityId, int dbZone, ExpRewardReceipt<BigDecimal> receipt, IGameCause cause, String remark) {
		String ownerName = this.getOwnerName(entityId);
		BaseRepository<Log> repository = findLogRepository(dbZone);
		
		for(RewardReceipt<BigDecimal>.RewardDetail detail : receipt.getDetails()) {
			Log log = newLog();
			log.setOwnerId(entityId);
			log.setOwnerName(ownerName);
			log.setCurr(this.getCount(entityId, detail.getThingId()));
			log.setDelta((BigDecimal)detail.getNum());
			log.setGameCause(cause);
			log.setThingId(detail.getThingId());
			log.setThingName(thingName(detail.getThingId()));
			log.setRemark(remark);
			log.setOldLevel(receipt.getOldLevel());
			log.setCurrLevel(receipt.getCurrLevel());
			repository.insert(log);
		}
	}
	
	@Override
	protected final void afterCost(long entityId, int dbZone, CostReceipt<BigDecimal> receipt, IGameCause cause, String remark) {
		throw new UnsupportedOperationException("exp thing " + thingType().getDesc() + " can't cost");
	}
	
	@Override
	protected CostReceipt<BigDecimal> doCost(long entityId, List<ICostThing<BigDecimal>> costThings, IGameCause cause,
			String remark) {
		throw new UnsupportedOperationException("exp thing " + thingType().getDesc() + " can't cost");
	}
	
	protected String thingName(int thingId) {
		return "";
	}
}
